<?php

/**
 * 开放商户 - 进件取消功能使用示例
 */

require_once __DIR__ . '/../../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Core\DemoConfig;
use JLPay\Services\Openmerch\Register\RegisterService;
use JLPay\Models\Openmerch\Register\IncomingCancelRequest;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

// =================== 日志配置 =====================
$logger = Logger::getInstance();

// =================== 工具函数 =====================
function createConfig(): OrgConfig
{
    return DemoConfig::createConfig();
}

// =================== 数据构建函数 =====================
function buildIncomingCancelRequest(): IncomingCancelRequest
{
    $request = new IncomingCancelRequest();
    
    // 设置请求参数
    $request->setApplyId("lcy_incoming_uat_20250630_0001");
    
    return $request;
}

// =================== 示例函数 =====================
function incomingCancelDemo(): void
{
    global $logger;

    $logger->info("开始执行进件取消示例");
    
    // 创建配置对象和注册服务
    $config = createConfig();
    $registerService = new RegisterService($config);
    
    // 创建进件取消请求
    $request = buildIncomingCancelRequest();
    
    // 发送请求 - 使用DemoRequestHandler
    $response = DemoRequestHandler::sendRequestAndHandleResponse(
        $registerService, 
        $request, 
        "进件取消",
        function($service, $request) {
            return $service->cancelIncoming($request);
        }
    );
    
    // 处理响应结果
    if ($response && $response->isSuccess()) {
        $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
    
    $logger->info("进件取消示例执行完毕");
}

// =================== 主程序 =====================
$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 进件取消示例 开始 =====");
$logger->info("=" . str_repeat("=", 50));

incomingCancelDemo();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 进件取消示例执行完毕 =====");
$logger->info("=" . str_repeat("=", 50)); 